1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 */ 54 package net.sf.cglib.core; 55 56 import junit.framework.*; 57 import java.util.*; 58 59 /*** 60 * @author Chris Nokleberg <a href="mailto:chris@nokleberg.com">chris@nokleberg.com</a> 61 * @version $Id: TestKeyFactory.java,v 1.4 2003/10/05 00:54:35 herbyderby Exp $ 62 */ 63 public class TestKeyFactory extends net.sf.cglib.CodeGenTestCase { 64 public interface MyKey { 65 public Object newInstance(int a, int[] b, boolean flag); 66 } 67 68 public interface MyKey2 { 69 public Object newInstance(int[][] a); 70 } 71 72 public interface CharArrayKey { 73 public Object newInstance(char[] a); 74 } 75 76 public interface BooleanArrayKey { 77 public Object newInstance(boolean[] a); 78 } 79 80 public interface ClassArrayKey { 81 public Object newInstance(Class[] a); 82 } 83 84 public interface MethodKey { 85 public Object newInstance(Class returnType, Class[] parameterTypes); 86 } 87 88 public interface PrimitivesKey { 89 public Object newInstance(boolean b, double d, float f, int i, long l); 90 } 91 92 public interface IntegerKey { 93 public Object newInstance(int i); 94 } 95 96 public interface LongKey { 97 public Object newInstance(long l); 98 } 99 100 public interface FloatKey { 101 public Object newInstance(float f); 102 } 103 104 public void testSimple() throws Exception { 105 MyKey mykey = (MyKey)KeyFactory.create(MyKey.class); 106 assertTrue(mykey.newInstance(5, new int[]{ 6, 7 }, false).hashCode() == 107 mykey.newInstance(5, new int[]{ 6, 7 }, false).hashCode()); 108 } 109 110 private Object helper(Class type) { 111 KeyFactory.Generator gen = new KeyFactory.Generator(); 112 gen.setInterface(type); 113 gen.setHashConstant(5); 114 gen.setHashMultiplier(3); 115 return gen.create(); 116 } 117 118 public void testPrimitives() throws Exception { 119 PrimitivesKey factory = (PrimitivesKey)helper(PrimitivesKey.class); 120 Object instance = factory.newInstance(true, 1.234d, 5.678f, 100, 200L); 121 assertTrue(instance.hashCode() == 1525582882); 122 } 123 124 public void testInteger() throws Exception { 125 IntegerKey factory = (IntegerKey)helper(IntegerKey.class); 126 Object instance = factory.newInstance(7); 127 assertTrue(instance.hashCode() == 22); 128 } 129 130 public void testLong() throws Exception { 131 LongKey factory = (LongKey)helper(LongKey.class); 132 Object instance = factory.newInstance(7L); 133 assertTrue(instance.hashCode() == 22); 134 } 135 136 public void testFloat() throws Exception { 137 FloatKey factory = (FloatKey)helper(FloatKey.class); 138 Object instance = factory.newInstance(7f); 139 assertTrue(instance.hashCode() == 1088421903); 140 } 141 142 public void testNested() throws Exception { 143 KeyFactory.Generator gen = new KeyFactory.Generator(); 144 gen.setInterface(MyKey2.class); 145 gen.setHashConstant(17); 146 gen.setHashMultiplier(37); 147 MyKey2 mykey2 = (MyKey2)gen.create(); 148 Object instance = mykey2.newInstance(new int[][]{ { 1, 2 }, { 3, 4 } }); 149 assertTrue(instance.hashCode() == 31914243); 150 } 151 152 public void testCharArray() throws Exception { 153 CharArrayKey f = (CharArrayKey)KeyFactory.create(CharArrayKey.class); 154 Object key1 = f.newInstance(new char[]{ 'a', 'b' }); 155 Object key2 = f.newInstance(new char[]{ 'a', '_' }); 156 assertTrue(!key1.equals(key2)); 157 } 158 159 public void testBooleanArray() throws Exception { 160 BooleanArrayKey f = (BooleanArrayKey)KeyFactory.create(BooleanArrayKey.class); 161 Object key1 = f.newInstance(new boolean[]{ true, false, true }); 162 Object key2 = f.newInstance(new boolean[]{ true, false, true }); 163 assertTrue(key1.equals(key2)); 164 } 165 166 public void testMethodKey() throws Exception { 167 MethodKey factory = (MethodKey)KeyFactory.create(MethodKey.class); 168 Set methodSet = new HashSet(); 169 methodSet.add(factory.newInstance(Number.class, new Class[]{ int.class })); 170 assertTrue(methodSet.contains(factory.newInstance(Number.class, new Class[]{ int.class }))); 171 assertTrue(!methodSet.contains(factory.newInstance(Number.class, new Class[]{ Integer.class }))); 172 } 173 174 public void testEqualOtherClass() throws Exception { 175 MyKey mykey = (MyKey)KeyFactory.create(MyKey.class); 176 assertTrue(!mykey.newInstance(5, new int[]{ 6, 7 }, false).equals(new Object())); 177 } 178 179 public TestKeyFactory(String testName) { 180 super(testName); 181 } 182 183 public static void main(String[] args) { 184 junit.textui.TestRunner.run(suite()); 185 } 186 187 public static Test suite() { 188 return new TestSuite(TestKeyFactory.class); 189 } 190 }

This page was automatically generated by Maven